package com.taptech.doufu.ui.view.personalcenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.ui.activity.NovelSectionDetailsActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.BaseBean;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.PersonalNoteBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.viewholder.personalcenter.BaseAdapterViewHolder;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.viewholder.personalcenter.PersonalNoteViewHolder;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.ui.view.DiaobaoTextView;

/**
 * Created by lsy on 2015/7/1.
 */
public class OldPersonalNoteViewHolder extends BaseAdapterViewHolder implements View.OnClickListener, View.OnLongClickListener
{

    /**
     * UI控件
     */
    public LinearLayout mContentBackgroudView;
    public ImageView mUserHeadImg, mUserMedalImg;
    //用户名和发表时间
    public TextView mUserName, mUserUgcTime;
    //内容来源
    public TextView mCircleSource;
    //内容标题
    public TextView mNoteTitle;
    public DiaobaoTextView mNoteContent;
    //图片及数目
    public SimpleDraweeView mContentImgeView;
    public LinearLayout mContentImgNumLayout;
    public TextView mContentImgNum;
    //浏览、献菊、评论数
    public TextView mReadCount, mFlowerCount, mCommentCount, mDeleteBtn, novel_content_textview;
    // gif标志
    public ImageView mGifSignIcon;
    // 右上角特殊标志
    public ImageView mRightTopFlagImg;

    LinearLayout flowerLayout, commentLayout, scanCountLayout;

    /**
     * 数据
     */
    protected PersonalNoteBean bean;


    protected Handler mHandler;
    public static final int TARGET_TYPE_NOTE_TO_NOVEL_DES = 11;


    public OldPersonalNoteViewHolder(Context context, int position)
    {
        super(context, position, R.layout.personal_note_viewholder_layout);
    }

    @Override
    public BaseAdapterViewHolder createViewHolder(Context context, int position)
    {

        return new OldPersonalNoteViewHolder(context, position);
    }

    @Override
    public void getChildView(View convertView)
    {

        mContentBackgroudView = (LinearLayout) convertView.findViewById(R.id.home_item_subject_image);
        mUserHeadImg = (ImageView) convertView.findViewById(R.id.home_item_subject_authorimg);
        mUserHeadImg.setOnClickListener(this);
        mUserMedalImg = (ImageView) convertView.findViewById(R.id.home_item_subject_medal);
        mUserName = (TextView) convertView.findViewById(R.id.home_item_subject_username);
        mUserUgcTime = (TextView) convertView.findViewById(R.id.home_item_subject_time);
        mCircleSource = (TextView) convertView.findViewById(R.id.home_item_subject_circle_source);
        mNoteTitle = (TextView) convertView.findViewById(R.id.personalnote_item_title_textview);
        mNoteContent = (DiaobaoTextView) convertView.findViewById(R.id.personal_item_content_textview);

        mNoteContent.setClickMode((byte) (DiaobaoTextView.AtClickEnable | DiaobaoTextView.LinkClickEnable));
        mContentImgeView = (SimpleDraweeView) convertView.findViewById(R.id.personal_note_image);
        mContentImgNumLayout = (LinearLayout) convertView.findViewById(R.id.content_img_num_layout);
        mContentImgNum = (TextView) convertView.findViewById(R.id.content_img_num);
        mReadCount = (TextView) convertView.findViewById(R.id.home_item_subject_readtimes);
        mFlowerCount = (TextView) convertView.findViewById(R.id.home_contents_item_flower_counts);
        mCommentCount = (TextView) convertView.findViewById(R.id.home_item_subject_circle_chatcount);
        mDeleteBtn = (TextView) convertView.findViewById(R.id.delete_item_btn);
        mGifSignIcon = (ImageView) convertView.findViewById(R.id.image_gif_sign_icon);

        mRightTopFlagImg = (ImageView) convertView.findViewById(R.id.home_rank_position);

        novel_content_textview = (TextView) convertView.findViewById(R.id.novel_content_textview);
        flowerLayout = (LinearLayout) convertView.findViewById(R.id.home_contents_item_praise_layout);
        commentLayout = (LinearLayout) convertView.findViewById(R.id.item_comment_layout);
        scanCountLayout = (LinearLayout) convertView.findViewById(R.id.scan_count_layout);
    }

    private void setBottomLayoutVisiable(boolean visiable)
    {
        if (visiable)
        {
            flowerLayout.setVisibility(View.VISIBLE);
            commentLayout.setVisibility(View.VISIBLE);
            scanCountLayout.setVisibility(View.VISIBLE);
        } else
        {
            flowerLayout.setVisibility(View.GONE);
            commentLayout.setVisibility(View.GONE);
            scanCountLayout.setVisibility(View.GONE);
        }

    }


    @Override
    public void setChildView(Object object,int singlePage)
    {

        mContentBackgroudView.setOnClickListener(this);
        mContentBackgroudView.setOnLongClickListener(this);
        mNoteContent.setOnClickListener(this);
        mNoteContent.setOnLongClickListener(this);
        if (mDeleteBtn != null)
        {
            mDeleteBtn.setOnClickListener(this);
        }
        bean = (PersonalNoteBean) object;
        handleViewDisplay(bean);

    }


    @Override
    public void setChildView(Object object, String keyWord,int singlePage)
    {
        mContentBackgroudView.setOnClickListener(this);
        if (mDeleteBtn != null)
        {
            mDeleteBtn.setOnClickListener(this);
        }


        bean = (PersonalNoteBean) object;
        handleViewDisplay(bean);
        if (bean == null)
        {

            return;
        }
        //头像及用户名
        mUserHeadImg.setImageResource(R.drawable.default_user_portrait);
        if (bean.getUser() != null)
        {

            GlideUtil.displayCircleImage(mUserHeadImg, bean.getUser().getIcon());
            DiaobaoUtil.setMedalImgView(bean.getUser().getMedal(), mUserMedalImg);
            mUserName.setText(DiaobaoUtil.textLighterColor(bean.getUser().getName(), keyWord));
        }

        // 日常标题
        if (bean.getTitle() != null)
        {
            mNoteTitle.setText(DiaobaoUtil.textLighterColor(bean.getTitle(), keyWord));
            if (bean.getTitle().equals(""))
            {

                mNoteTitle.setVisibility(View.GONE);
            } else
            {
                mNoteTitle.setVisibility(View.VISIBLE);
            }

        } else
        {

            mNoteTitle.setVisibility(View.GONE);
        }
        // 日常内容
        if (bean.getContent() != null)
        {
            mNoteContent.setText(DiaobaoUtil.textLighterColor(bean.getContent() + " ", keyWord));
            if (bean.getContent().equals(""))
            {


                mNoteContent.setVisibility(View.GONE);
            } else
            {
                mNoteContent.setVisibility(View.VISIBLE);
            }
        } else
        {
            mNoteContent.setVisibility(View.GONE);
        }
    }

    @Override
    public void setHandler(Handler handler)
    {

        mHandler = handler;

    }

    private void handleViewDisplay(final PersonalNoteBean contentBean)
    {
        if (contentBean == null)
        {

            return;
        }
        //头像及用户名
        mUserHeadImg.setImageResource(R.drawable.default_user_portrait);
        if (contentBean.getUser() != null)
        {
            GlideUtil.displayCircleImage(mUserHeadImg, contentBean.getUser().getIcon());
            DiaobaoUtil.setMedalImgView(contentBean.getUser().getMedal(), mUserMedalImg);
            mUserName.setText(contentBean.getUser().getName());
        }
        if (contentBean.getCreated_at() != null)
        {
            if (contentBean.getCreated_at().contains(" "))
            {

                mUserUgcTime.setText(contentBean.getCreated_at());
            } else
            {
                mUserUgcTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(contentBean.getCreated_at())));
            }
        }
        if (contentBean.getObject_type() != null)
        {
            if (contentBean.getObject_type().equals(PersonalNoteService.OBJECT_TYPE_NOTE))
            {

                mCircleSource.setText("日常");
            } else if (contentBean.getObject_type().equals(PersonalNoteService.OBJECT_TYPE_DRAW))
            {
                if (contentBean.getDrawing_type().equals("1"))
                {
                    mCircleSource.setText("插画");
                } else
                {
                    mCircleSource.setText("漫画");
                }
            } else if (contentBean.getObject_type().equals(PersonalNoteService.OBJECT_TYPE_COSER))
            {
                mCircleSource.setText("COS");
            }
        }
        if (contentBean.getTitle() != null)
        {
            mNoteTitle.setText(contentBean.getTitle());
            if (contentBean.getTitle().equals(""))
            {

                mNoteTitle.setVisibility(View.GONE);
            } else
            {
                mNoteTitle.setVisibility(View.VISIBLE);
            }

        } else
        {

            mNoteTitle.setVisibility(View.GONE);
        }
        TTLog.d("tag", "the value is======================" + contentBean.getContent());
        if (contentBean.getContent() != null)
        {
            mNoteContent.setDiaobaoText(contentBean.getContent() + " ", PersonalNoteViewHolder.TEXT_DES_MAX_LENGTH);
            if (contentBean.getContent().equals(""))
            {


                mNoteContent.setVisibility(View.GONE);
            } else
            {
                mNoteContent.setVisibility(View.VISIBLE);
            }
        } else
        {
            mNoteContent.setVisibility(View.GONE);
        }
        // 内容是否包含图片
        if (contentBean.getCover() != null && !contentBean.getCover().toString().equals("null"))
        {


            mContentImgeView.setVisibility(View.VISIBLE);
            String headUrl = "http://imgold2.doufu.la/";
            String imgUrl = headUrl + contentBean.getCover().getCrop_path();
            int cropImgWidth = Integer.valueOf(contentBean.getCover().getCrop_width());
            int cropImgHeight = Integer.valueOf(contentBean.getCover().getCrop_height());
            float imageAspetctRatio = cropImgWidth / (float) cropImgHeight;


            if (imageAspetctRatio < 1)
            {
                imageAspetctRatio = 1;
            }
            mContentImgeView.setBackgroundResource(R.drawable.biankuang_no_image);
            ImageManager.loadImage(mContentImgeView, imgUrl, imageAspetctRatio, new BaseControllerListener<ImageInfo>()
            {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable)
                {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (contentBean.getImages().length > 1)
                    {
                        mContentImgNumLayout.setVisibility(View.VISIBLE);
                        mContentImgNum.setText(contentBean.getImages().length + "P");
                    } else
                    {
                        mContentImgNumLayout.setVisibility(View.GONE);
                    }
                }
            });
            // 如果是gif则显示标志
            if (DiaobaoUtil.isImageUrlGif(imgUrl))
            {
                mGifSignIcon.setVisibility(View.VISIBLE);
            } else
            {
                mGifSignIcon.setVisibility(View.GONE);
            }

        } else
        {

            mContentImgNumLayout.setVisibility(View.GONE);
            mContentImgeView.setVisibility(View.GONE);
        }


        if (contentBean.getRead_times() != null)
        {
            mReadCount.setText(contentBean.getRead_times());
        }
        if (contentBean.getFlower_count() != null)
        {
            mFlowerCount.setText(contentBean.getFlower_count());
        }
        if (contentBean.getComment_times() != null)
        {

            mCommentCount.setText(contentBean.getComment_times());
        }
        if (AccountService.getInstance().isUserSelf(contentBean.getUser_id()))
        {
            mDeleteBtn.setVisibility(View.VISIBLE);
        } else
        {
            mDeleteBtn.setVisibility(View.GONE);
        }

        if (contentBean.getSpecialFlag() == BaseBean.HOME_NOTE_RECOMMEND)
        {
            mRightTopFlagImg.setImageResource(R.drawable.home_note_recommend_img);
            mRightTopFlagImg.setVisibility(View.VISIBLE);
        } else
        {
            mRightTopFlagImg.setVisibility(View.GONE);
        }


        //如果是小说更新
        if (bean.getNovel() != null && DiaobaoUtil.String2Int(bean.getTarget_type()) == TARGET_TYPE_NOTE_TO_NOVEL_DES)
        {
            MineNovelBean novelBean = bean.getNovel();
            if (novelBean == null)
            {
                return;
            }
            setBottomLayoutVisiable(false);
            novel_content_textview.setVisibility(View.VISIBLE);
            mCircleSource.setText("小说");
            if (novelBean.getFinished() != null)
            {
                mNoteTitle.setText(novelBean.getTitle());
            }
            mNoteContent.setText(bean.getTitle());
            novel_content_textview.setText(bean.getContent());

            if (novelBean.getFinished() != null)
            {
                int drawableId = getNovelStatusIma(novelBean.getFinished());
                if (novelBean.getFinished().equals("0"))
                {
                  /*  novel_end_iv.setVisibility(View.GONE);
                    novel_continue_iv.setVisibility(View.VISIBLE);*/
                    Drawable drawable1 = mContext.getResources().getDrawable(drawableId);
                    // drawable1.setBounds(ScreenUtil2.dip2px(6), 0, ScreenUtil2.dip2px(42), ScreenUtil2.dip2px(15));//第一0是距左边距离，第二0是距上边距离
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());//第一0是距左边距离，第二0是距上边距离
                    mNoteTitle.setCompoundDrawables(null, null, drawable1, null);
                } else
                {
                   /* novel_continue_iv.setVisibility(View.GONE);
                    novel_end_iv.setVisibility(View.VISIBLE);*/
                    Drawable drawable1 = mContext.getResources().getDrawable(drawableId);
                    // drawable1.setBounds(ScreenUtil2.dip2px(6), 0, ScreenUtil2.dip2px(31), ScreenUtil2.dip2px(15));//第一0是距左边距离，第二0是距上边距离
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());//第一0是距左边距离，第二0是距上边距离
                    mNoteTitle.setCompoundDrawables(null, null, drawable1, null);
                }

            }
            if (bean.getCreated_at().contains(" "))
            {

                mUserUgcTime.setText(contentBean.getCreated_at());
            } else
            {
                mUserUgcTime.setText("更新章节：" + DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getCreated_at())));
            }
        } else
        {
            setBottomLayoutVisiable(true);
            mNoteTitle.setCompoundDrawables(null, null, null, null);
            novel_content_textview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.home_item_subject_authorimg:
                UserDetailActivity.Companion.startActivity(mContext,bean.getUser_id());
                break;
            case R.id.delete_item_btn:
                if (AccountService.getInstance().isUserSelf(bean.getUser_id()))
                {

                    Message message = new Message();
                    message.what = BaseAdapterViewHolder.DELETE_NOTE_ITEM;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);
                    message.setData(bundle);
                    if (mHandler != null)
                    {
                        mHandler.sendMessage(message);
                    }
                }
                break;
           /* default:

                TTLog.d("tag", "the note will be ENTER ===================");
                PersonalNoteService.getInstance().enterPersonalNoteDesActivity(mContext, bean.getId(), bean.getObject_type());
                break;*/
            default:
                int target_type = DiaobaoUtil.String2Int(bean.getTarget_type());
                switch (target_type)
                {
                    case TARGET_TYPE_NOTE_TO_NOVEL_DES:
                        Intent temp_intent = new Intent(mContext, NovelSectionDetailsActivity.class);
                        temp_intent.putExtra(Constant.ARTICLE_NOVELID, bean.getObject_id());//topic id
                        temp_intent.putExtra(Constant.ARTICLE_ID, bean.getSubobject_id());// details id
                        temp_intent.putExtra(Constant.IS_SELF, true);
                        mContext.startActivity(temp_intent);
                        // StartActivityUtil.novelOnclick(mContext, contentBean.getObject_id());
                        break;
                   /* case 18:
                    case 28:
                        SweepFlowerService.enterSweepDes(mContext, bean.getObject_id(), bean.getTarget_type());
                        break;*/
                    default:
                        TTLog.d("tag", "the note will be ENTER ==================");
                        PersonalNoteService.getInstance().enterPersonalNoteDesActivity(mContext, bean.getId(), bean.getObject_type());
                        break;
                }

                break;
        }


    }

    @Override
    public boolean onLongClick(View view)
    {
        switch (view.getId())
        {
            case R.id.personal_item_content_textview:
            case R.id.home_item_subject_image:
                if (AccountService.getInstance().isUserSelf(bean.getUser_id()))
                {

                    Message message = new Message();
                    message.what = BaseAdapterViewHolder.DELETE_NOTE_ITEM;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);
                    message.setData(bundle);
                    if (mHandler != null)
                    {
                        mHandler.sendMessage(message);
                    }
                }
                break;
            default:

                break;
        }
        return true;
    }

    private int  getNovelStatusIma(String status){
        int drawable1Id = 0;
        if("0".equals(status)){
             if(WeMediaApplication.getInstance().isDayNightMode_Night){
                 drawable1Id = R.drawable.icon_novel_continue_night;
             }else {
                 drawable1Id = R.drawable.icon_novel_continue;
             }
        }else {
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                drawable1Id =   R.drawable.icon_novel_end_night;
            }else {
                drawable1Id =   R.drawable.icon_novel_end;
            }
        }
        return drawable1Id;
    }
}